<?php
namespace app\serversubmitform\logic;

use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;
use \think\Db;

/**
 * @author:      李颖
 * @DateTime:    2017-09-04 14:03:14
 * @Description: 提交表单 - 意见建议
 */
class Submitform extends BaseLogic
{
	protected $name = 'submitform';
    protected $config = 'db_config_submitform';
    protected $submitformLogic = 'app\serversubmitform\logic\Submitform';
    protected $submitformModel = 'app\serversubmitform\model\Submitform';

    /*我的 - 提交意见建议*/
    public function suggestion($data): array 
    {
        $form_content = [];
        foreach ($data as $k => $v) {
            if(!in_array($k, ['user_id','user_info','app_id'])){
                $form_content[$k] = $v;
            }
        }
        $data['form_content'] = $form_content;
        $res = $this->model($this->submitformModel)->allowField(['app_id','user_id','user_info','form_content','create_time','update_time'])->save($data);
        return auto_return($res);

    }

    /*后台 - 获取意见建议列表*/
    public function getSuggestionList($data): array 
    {
        $where['status'] = 1;
        $manageAppId = ApiBase::getAppId();
        if($data['app_id'] != $manageAppId){ $where['app_id'] = $data['app_id']; }
        $data['keywords'] !== '' && $where['user_info$.name'] = ['like', '%' . $data['keywords'] . '%'];
        $res = $this->model($this->submitformModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到意见建议列表数据', '没有意见建议列表数据');
    }

    /*标记某表单已阅读*/
    public function remarkRead($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['is_read'] = 0;
        $_where['status'] = 1;
        $res = $this->model($this->submitformModel)->save(['is_read'=>1], $_where);
        return auto_return($res);

    }

}